﻿using System;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace sy4_10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "抽奖程序 - 候选名单管理";

            // 添加一些初始候选名单
            listBox1.Items.Add("小王");
            listBox1.Items.Add("李峰");
            listBox1.Items.Add("小张");
            listBox1.Items.Add("刘明");
            listBox1.Items.Add("赵雪");
        }

        // 添加候选人
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text.Trim();

            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("请输入姓名！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (listBox1.Items.Contains(name))
            {
                MessageBox.Show("该候选人已存在！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            listBox1.Items.Add(name);
            textBox1.Text = "";
            textBox1.Focus();

            UpdateStatus();
        }

        // 删除候选人
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                MessageBox.Show("请选择要删除的候选人！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string selectedName = listBox1.SelectedItem.ToString();
            DialogResult result = MessageBox.Show($"确定要删除候选人 '{selectedName}' 吗？",
                "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                UpdateStatus();
            }
        }

        // 去抽奖
        private void buttonLottery_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0)
            {
                MessageBox.Show("候选名单为空，请先添加候选人！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (listBox1.Items.Count < 2)
            {
                MessageBox.Show("候选名单至少需要2人才能进行抽奖！", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 使用重载构造函数传递候选名单
            Form2 form2 = new Form2(listBox1);
            form2.ShowDialog();
        }

        // 更新状态
        private void UpdateStatus()
        {
            this.Text = $"抽奖程序 - 候选名单管理 (共{listBox1.Items.Count}人)";
        }

        // 文本框按键事件 - 按Enter键添加
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                buttonAdd_Click(sender, e);
                e.Handled = true;
            }
        }
    }
}